home *** CD-ROM | disk | FTP | other *** search
/ Alde ADA 1: #1 / CCCC 8804 Volume 1 Number 1 - Alde.iso / C / MISC / FUNC / PROFF.ARC / LOOK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-21  |  3.5 KB  |  158 lines

  1. /*
  2.  * from K&R "The C Programming language"
  3.  * Table lookup routines
  4.  *
  5.  */
  6. #include <stdio.h>
  7. #include "lookup.h"
  8. /*
  9.  * hash - for a hash value for string s
  10.  *
  11.  */
  12. hash(s)
  13. char *s;
  14. {
  15.    int hashval;
  16.  
  17.    for (hashval = 0; *s != '\0';)
  18.       hashval += *s++;
  19.    return (hashval % HASHMAX);
  20. }
  21.  
  22. /*
  23.  * lookup - lookup for a string s in the hash table
  24.  *
  25.  */
  26. struct hashlist *lookup(s, hashtab)
  27. char *s;
  28. struct hashlist *hashtab[];
  29. {
  30.    struct hashlist *np;
  31.  
  32.    for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  33.       if (strcmp(s, np->name) == 0)
  34.          return (np);                  /* found     */
  35.    return (NULL);                      /* not found */
  36. }
  37.  
  38. /*
  39.  * install - install a string name in hashtable and its value def
  40.  * at a given hashtable.
  41.  */
  42. struct hashlist    *install(name, def, hashtab)
  43. char *name;
  44. char *def;
  45. struct hashlist *hashtab[];
  46. {
  47.    int hashval;
  48.    struct hashlist *np, *lookup();
  49.    char *strsave(), *malloc();
  50.  
  51.    if ((np = lookup(name, hashtab)) == NULL)
  52.    {                                   /* not found.. */
  53.       np = (struct hashlist *) malloc(sizeof(*np));
  54.       if (np == NULL)
  55.          return (NULL);
  56.       if ((np->name = strsave(name)) == NULL)
  57.          return (NULL);
  58.       hashval = hash(np->name);
  59.       np->next = hashtab[hashval];
  60.       hashtab[hashval] = np;
  61.    }
  62.    else                                /* found..     */
  63.       free(np->def);                   /* free prev.  */
  64.    if ((np->def = strsave(def)) == NULL)
  65.       return (NULL);
  66.    return (np);
  67. }
  68.  
  69. /*
  70.  * strsave - save string s somewhere
  71.  *
  72.  */
  73. char *strsave(s)
  74. char *s;
  75. {
  76.    char *p, *malloc();
  77.    register int n;
  78.  
  79.    n = strlen(s) + 1;
  80.    if ((p = malloc(n)) != NULL)
  81.       strcpy(p, s);
  82.    return (p);
  83. }
  84.  
  85. /*
  86.  * lexinstal - instal a string name in hashtable and its value
  87.  *             used by lexical analyser to quickly match a token
  88.  *             and return its lexical value.
  89.  *
  90.  */
  91. struct lexlist *lexinstal(name, val, flag, lextable)
  92. char *name;
  93. int val;
  94. int flag;
  95. struct lexlist *lextable[];
  96. {
  97.    int hashval;
  98.    struct lexlist *np, *lexlook();
  99.    char *strsave(), *malloc();
  100.  
  101.    if ((np = lexlook(name, lextable)) == NULL)
  102.    {                                   /* not found.. */
  103.       np = (struct lexlist *) malloc(sizeof(*np));
  104.       if (np == NULL)
  105.          return (NULL);
  106.       if ((np->name = strsave(name)) == NULL)
  107.          return (NULL);
  108.       hashval = hash(np->name);
  109.       np->link = lextable[hashval];
  110.       lextable[hashval] = np;
  111.    }
  112.    np->val = val;                      /* replace prev */
  113.    np->flag = flag;
  114.    return (np);
  115. }
  116.  
  117. /*
  118.  * lexlook - lookup for a string s in the hash table
  119.  *           used by lexinstal only.
  120.  *
  121.  */
  122. struct lexlist *lexlook(s, table)
  123. char *s;
  124. struct lexlist *table[];
  125. {
  126.    struct lexlist *np;
  127.  
  128.    for (np = table[hash(s)]; np != NULL; np = np->link)
  129.       if (strcmp(s, np->name) == 0)
  130.          return (np);                  /* found     */
  131.    return (NULL);                      /* not found */
  132. }
  133.  
  134. /*
  135.  * remove an item from the hash table forever
  136.  *
  137.  */
  138. struct lexlist *remove_item(s, table)
  139. char *s;
  140. struct lexlist *table[];
  141. {
  142.    struct lexlist *np, *xp;
  143.  
  144.    np = table[hash(s)];
  145.    xp = np;
  146.    while (np != NULL)
  147.    {
  148.       if (strcmp(s, np->name) == 0)
  149.       {
  150.          xp->link = np->link;          /* remove the link */
  151.          return (np);                  /* return the lost */
  152.       }
  153.       xp = np;
  154.       np = np->link;
  155.    }
  156.    return (NULL);
  157. }
  158.